package com.sso.core.store;

import com.sso.core.user.SsoUser;
import com.sso.core.conf.SSOConf;
import com.sso.core.util.JedisUtil;


public class SsoLoginStore {

    private static int redisExpireMinute = 1440;    // 1440 minute, 24 hour

    public static void setRedisExpireMinute(int redisExpireMinute) {
        if (redisExpireMinute < 30) {
            redisExpireMinute = 30;
        }
        SsoLoginStore.redisExpireMinute = redisExpireMinute;
    }

    public static int getRedisExpireMinute() {
        return redisExpireMinute;
    }

    /**
     * get ssoUser
     *
     * @param storeKey
     * @return
     */
    public static SsoUser get(String storeKey) {

        String redisKey = ticketKey(storeKey);
        Object objectValue = JedisUtil.getObjectValue(redisKey);
        if (objectValue != null) {
            SsoUser ssoUser = (SsoUser) objectValue;
            return ssoUser;
        }
        return null;
    }

    /**
     * remove
     *
     * @param storeKey
     */
    public static void remove(String storeKey) {
        String redisKey = ticketKey(storeKey);
        JedisUtil.del(redisKey);
    }

    /**
     * put ssoUser
     *
     * @param storeKey
     * @param ssoUser
     */
    public static void put(String storeKey, SsoUser ssoUser) {
        String redisKey = ticketKey(storeKey);
        JedisUtil.setObjectValue(redisKey, ssoUser, redisExpireMinute * 60);  // minute to second
    }


    private static String ticketKey(String sessionId) {
        return SSOConf.SSO_SESSIONID.concat("#").concat(sessionId);
    }

    private static String smsCodeKey(String sessionId) {
        return SSOConf.SSO_SMS_CODE.concat("#").concat(sessionId);
    }


    public static void putSmsCode(String storeKey, String value, int minute) {
        String redisKey = smsCodeKey(storeKey);
        JedisUtil.setStringValue(redisKey, value, minute * 60);  // minute to second
    }

    public static String getSmsCode(String storeKey) {
        String redisKey = smsCodeKey(storeKey);
        String value = JedisUtil.getStringValue(redisKey);
        return value;
    }


}
